home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_gimp.idb / usr / freeware / share / gimp / scripts / perspective-shadow.scm.z / perspective-shadow.scm
Encoding:
GIMP Script-Fu Script  |  1999-07-21  |  6.1 KB  |  222 lines

  1. ; The GIMP -- an image manipulation program
  2. ; Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3. ; This program is free software; you can redistribute it and/or modify
  4. ; it under the terms of the GNU General Public License as published by
  5. ; the Free Software Foundation; either version 2 of the License, or
  6. ; (at your option) any later version.  
  7. ; This program is distributed in the hope that it will be useful,
  8. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. ; GNU General Public License for more details.
  11. ; You should have received a copy of the GNU General Public License
  12. ; along with this program; if not, write to the Free Software
  13. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. ;
  15. ;
  16. ; perspective-shadow.scm   version 1.02   12/13/97
  17. ;
  18. ; CHANGE-LOG:
  19. ; 1.00 - initial release
  20. ; 1.01 - fixed the problem with a remaining copy of the selection
  21. ; 1.02 - some code cleanup, no real changes
  22. ;
  23. ;
  24. ; Copyright (C) 1997 Sven Neumann (neumanns@uni-duesseldorf.de)
  25. ;  
  26. ; Adds a perspective shadow of the current selection or alpha-channel 
  27. ; as a layer below the active layer
  28. ;
  29.   
  30. (define (script-fu-perspective-shadow image 
  31.                       drawable
  32.                       alpha
  33.                       rel-distance
  34.                       rel-length
  35.                       shadow-blur
  36.                       shadow-color
  37.                       shadow-opacity
  38.                       interpolate
  39.                       allow-resize)
  40.   (let* ((shadow-blur (max shadow-blur 0))
  41.      (shadow-opacity (min shadow-opacity 100))
  42.      (shadow-opacity (max shadow-opacity 0))
  43.      (rel-length (abs rel-length))
  44.      (alpha (* (/ alpha 180) *pi*))
  45.      (type (car (gimp-drawable-type-with-alpha drawable)))
  46.      (image-width (car (gimp-image-width image)))
  47.      (image-height (car (gimp-image-height image)))
  48.      (old-bg (car (gimp-palette-get-background)))
  49.      (from-selection 0)
  50.      (active-selection 0)
  51.      (shadow-layer 0))
  52.  
  53.     
  54.   (if (= rel-distance 0) (set! rel-distance 999999)) 
  55.   (gimp-image-disable-undo image)
  56.   
  57.   (gimp-layer-add-alpha drawable)
  58.   (if (= (car (gimp-selection-is-empty image)) TRUE)
  59.       (begin
  60.     (gimp-selection-layer-alpha image drawable)
  61.     (set! from-selection FALSE))
  62.       (begin 
  63.     (set! from-selection TRUE)
  64.     (set! active-selection (car (gimp-selection-save image)))))
  65.   
  66.   (let* ((selection-bounds (gimp-selection-bounds image))
  67.      (select-offset-x (cadr selection-bounds))
  68.      (select-offset-y (caddr selection-bounds))
  69.      (select-width (- (cadr (cddr selection-bounds)) select-offset-x))
  70.      (select-height (- (caddr (cddr selection-bounds)) select-offset-y))
  71.  
  72.      (abs-length (* rel-length select-height))
  73.      (abs-distance (* rel-distance select-height))
  74.      (half-bottom-width (/ select-width 2)) 
  75.      (half-top-width (* half-bottom-width 
  76.               (/ (- rel-distance rel-length) rel-distance)))
  77.   
  78.      (x0 (+ select-offset-x (+ (- half-bottom-width half-top-width)
  79.                  (* (cos alpha) abs-length))))
  80.      (y0 (+ select-offset-y (- select-height
  81.                  (* (sin alpha) abs-length))))
  82.      (x1 (+ x0 (* 2 half-top-width)))
  83.      (y1 y0)
  84.      (x2 select-offset-x)
  85.      (y2 (+ select-offset-y select-height))
  86.      (x3 (+ x2 select-width))
  87.      (y3 y2)
  88.  
  89.      (shadow-width (+ (- (max x1 x3) (min x0 x2)) (* 2 shadow-blur)))
  90.      (shadow-height (+ (- (max y1 y3) (min y0 y2)) (* 2 shadow-blur)))
  91.      (shadow-offset-x (- (min x0 x2) shadow-blur))
  92.      (shadow-offset-y (- (min y0 y2) shadow-blur)))
  93.      
  94.   
  95.     (set! shadow-layer (car (gimp-layer-new image 
  96.                         select-width 
  97.                         select-height 
  98.                         type
  99.                         "Perspective Shadow" 
  100.                         shadow-opacity
  101.                         NORMAL)))
  102.     (gimp-layer-set-offsets shadow-layer select-offset-x select-offset-y)
  103.     (gimp-drawable-fill shadow-layer TRANS-IMAGE-FILL)
  104.     (gimp-palette-set-background shadow-color)
  105.     (gimp-edit-fill image shadow-layer)
  106.     (gimp-selection-none image)
  107.  
  108.     (if (= allow-resize TRUE)
  109.     (let* ((new-image-width image-width)
  110.            (new-image-height image-height)
  111.            (image-offset-x 0)
  112.            (image-offset-y 0))
  113.  
  114.       (if (< shadow-offset-x 0)
  115.           (begin
  116.         (set! image-offset-x (- 0 shadow-offset-x))
  117.         (set! new-image-width (- new-image-width image-offset-x))))
  118.  
  119.       (if (< shadow-offset-y 0)
  120.           (begin
  121.         (set! image-offset-y (- 0 shadow-offset-y))
  122.         (set! new-image-height (- new-image-height image-offset-y))))
  123.       
  124.       (if (> (+ shadow-width shadow-offset-x) new-image-width)
  125.           (set! new-image-width (+ shadow-width shadow-offset-x)))
  126.       
  127.       (if (> (+ shadow-height shadow-offset-y) new-image-height)
  128.           (set! new-image-height (+ shadow-height shadow-offset-y)))
  129.       (gimp-image-resize image
  130.                  new-image-width 
  131.                  new-image-height 
  132.                  image-offset-x 
  133.                  image-offset-y)))
  134.  
  135.     (gimp-image-add-layer image shadow-layer -1)
  136.   
  137.     (gimp-perspective image
  138.               shadow-layer
  139.               interpolate
  140.               x0 y0
  141.               x1 y1
  142.               x2 y2
  143.               x3 y3)
  144.  
  145.     (if (> shadow-blur 0) 
  146.     (begin
  147.       (gimp-layer-set-preserve-trans shadow-layer FALSE)
  148.       (gimp-layer-resize shadow-layer 
  149.                  shadow-width
  150.                  shadow-height
  151.                  shadow-blur
  152.                  shadow-blur)
  153.       (plug-in-gauss-rle 1 
  154.                  image 
  155.                  shadow-layer 
  156.                  shadow-blur 
  157.                  TRUE 
  158.                  TRUE))))
  159.  
  160.   (if (= from-selection TRUE)
  161.       (begin
  162.     (gimp-selection-load image active-selection)
  163.     (gimp-edit-clear image shadow-layer)
  164.     (gimp-image-remove-channel image active-selection)))
  165.  
  166.   (if (and 
  167.        (= (car (gimp-layer-is-floating-sel drawable)) 0)
  168.        (= from-selection FALSE))
  169.       (gimp-image-raise-layer image drawable))
  170.  
  171.   (gimp-image-set-active-layer image drawable)
  172.   (gimp-palette-set-background old-bg)
  173.   (gimp-image-enable-undo image)
  174.   (gimp-displays-flush)))
  175.  
  176. (script-fu-register "script-fu-perspective-shadow" 
  177.             "<Image>/Script-Fu/Shadow/Perspective"
  178.             "Add a perspective shadow"
  179.             "Sven Neumann (neumanns@uni-duesseldorf.de)"
  180.             "Sven Neumann"
  181.             "12/13/1997"
  182.             "RGB RGBA GRAY GRAYA"
  183.             SF-IMAGE "Image" 0
  184.             SF-DRAWABLE "Drawable" 0
  185.             SF-VALUE "Angle" "45"
  186.             SF-VALUE "Relative horizon distance" "5.0"
  187.             SF-VALUE "Relative shadow length" "1.0"
  188.             SF-VALUE "Blur Radius" "3"
  189.             SF-COLOR "Color" '(0 0 0)
  190.             SF-VALUE "Opacity" "80"
  191.             SF-TOGGLE "Interpolate" TRUE
  192.             SF-TOGGLE "Allow Resizing" FALSE) 
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.